﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BanmaytinhBOL;
using BanmaytinhDAL;
using System.Text.RegularExpressions;
using Banmaytinh.Common;
namespace Banmaytinh.AdminSP
{
    public partial class Hangsanxuat : System.Web.UI.Page
    {
       // private static int indexOfObject = 0;
        private static List<HangsanxuatInfo> source;
        private const string INSERT = "Thêm";
        private const string UPDATE = "Cập nhật";
        private const string CMD_EDIT = "cmdEdit";
        private const string CMD_DELETE = "cmdDelete";
        private static string Mahangsanxuat = String.Empty;
        private static HangsanxuatInfo obj;
        private static  int IndexOfObject = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                LoadDataToList();
            
            }
        }
        protected void LoadDataToList()
        {
            source = new List<HangsanxuatInfo>();
            source = BanmaytinhBOL.HangsanxuatBOL.Hangsanxuat_GetByAll();
        }

        
        private void BindGrid() 
        {
            
            grHangsanxuat.DataSource = BanmaytinhBOL.HangsanxuatBOL.Hangsanxuat_GetByAll();
            grHangsanxuat.DataBind();
            
        }

        protected void lbtnXoa_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grHangsanxuat.Rows.Count; i++)
            {
                GridViewRow row = grHangsanxuat.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                    if (chk.Checked)
                    {
                        String ID = row.Cells[1].Text;
                        HangsanxuatBOL.Hangsanxuat_Delete(ID);

                    }
                   

                }
            
            }
            grHangsanxuat.PageIndex = 0;
            BindGrid();
        }
        protected void PanelOnOff()
        {
            Panelview.Visible = !Panelview.Visible;
            Paneledit.Visible = !Paneledit.Visible;
           
        }
        protected void lbtnThemmoi_Click(object sender, EventArgs e)
        {
            PanelOnOff();
            btnLuu.Text = INSERT;
        }

        protected void SetValueFromForm() 
        {
            if (obj == null)
                obj = new HangsanxuatInfo();
                obj.Tenhangsanxuat = txtTenhangsanxuat.Text;

        }
        protected void FillValueToForm(HangsanxuatInfo obj) 
        {
            txtTenhangsanxuat.Text = obj.Tenhangsanxuat;
        
        }
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SetValueFromForm();
                switch (btnLuu.Text)
                {
                    case UPDATE:
                        HangsanxuatBOL.Hangsanxuat_Update(obj);
                        BindGrid();
                        break;
                    case INSERT:
                        HangsanxuatBOL.Hangsanxuat_Insert(obj);
                        BindGrid();
                        break;
                }
                ControlHelper.ResetControl(Paneledit);
                PanelOnOff();
                obj = null;
            }
        }

        protected void grHangsanxuat_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grHangsanxuat.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grHangsanxuat_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Mahangsanxuat = e.CommandArgument.ToString();
            obj = new HangsanxuatInfo();
            obj = HangsanxuatBOL.Hangsanxuat_GetById(Mahangsanxuat);

            switch (e.CommandName)
            {
                case CMD_DELETE:
                    HangsanxuatBOL.Hangsanxuat_Delete(Mahangsanxuat);
                    BindGrid();
                    break;
                case CMD_EDIT:
                    FillValueToForm(obj);
                    IndexOfObject = source.FindIndex(delegate(HangsanxuatInfo tmp)
                        {
                            return tmp.Mahangsanxuat == Mahangsanxuat;
                        }
                    );
                    btnLuu.Text = UPDATE;
                    PanelOnOff();
                    break;
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            PanelOnOff();
            ControlHelper.ResetControl(Paneledit);
        }
        //protected void Insert(HangsanxuatInfo input)
        //{
        //    HangsanxuatDAL.Insert(input);
        //    source.Insert(0, input);
        //}
        //protected void Delete(HangsanxuatInfo input)
        //{
        //    HangsanxuatDAL.Delete(input.Mahangsanxuat);
        //    source.RemoveAt(source.FindIndex(delegate(HangsanxuatInfo tmp) { return tmp.Mahangsanxuat == input.Mahangsanxuat; }));
        //}
        //protected void Update(HangsanxuatInfo input)
        //{
        //    HangsanxuatDAL.Update(input);
        //    source[IndexOfObject] = input;
        //}

     }
}